//v.3.6 build 130417

/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
You allowed to use this component or parts of it under GPL terms
To use it on other terms or get Professional edition of the component please contact us at sales@dhtmlx.com
*/
if(!window.dhtmlx)
	window.dhtmlx = {};

(function(){
	var _dhx_msg_cfg = null;
	function callback(config, result){
			var usercall = config.callback;
			modality(false);
			if(config.box.parentNode)
			  config.box.parentNode.removeChild(config.box);
			_dhx_msg_cfg = config.box = null;
			if (usercall)
				usercall(result);
	}
	function modal_key(e){
		if (_dhx_msg_cfg){
			e = e||event;
			var code = e.which||event.keyCode;
			if (dhtmlx.message.keyboard){
				if (code == 13 || code == 32)
					callback(_dhx_msg_cfg, true);
				if (code == 27)
					callback(_dhx_msg_cfg, false);
			}
			if (e.preventDefault)
				e.preventDefault();
			return !(e.cancelBubble = true);
		}
	}
	if (document.attachEvent)
		document.attachEvent("onkeydown", modal_key);
	else
		document.addEventListener("keydown", modal_key, true);
		
	function modality(mode){
		if(!modality.cover){
			modality.cover = document.createElement("DIV");
			//necessary for IE only
			modality.cover.onkeydown = modal_key;
			modality.cover.className = "dhx_modal_cover";
			document.body.appendChild(modality.cover);
		}
		var height =  document.body.scrollHeight;
		modality.cover.style.display = mode?"inline-block":"none";
	}

	function button(text, result){
		return "<div class='dhtmlx_popup_button' result='"+result+"' ><div>"+text+"</div></div>";
	}

	function info(text){
		if (!t.area){
			t.area = document.createElement("DIV");
			t.area.className = "dhtmlx_message_area";
			t.area.style[t.position]="5px";
			document.body.appendChild(t.area);
		}

		t.hide(text.id);
		var message = document.createElement("DIV");
		message.innerHTML = "<div>"+text.text+"</div>";
		message.className = "dhtmlx-info dhtmlx-" + text.type;
		message.onclick = function(){
			t.hide(text.id);
			text = null;
		};

		if (t.position == "bottom" && t.area.firstChild)
			t.area.insertBefore(message,t.area.firstChild);
		else
			t.area.appendChild(message);
		
		if (text.expire > 0)
			t.timers[text.id]=window.setTimeout(function(){
				t.hide(text.id);
			}, text.expire);

		t.pull[text.id] = message;
		message = null;

		return text.id;
	}
	function _boxStructure(config, ok, cancel){
		var box = document.createElement("DIV");
		box.className = " dhtmlx_modal_box dhtmlx-"+config.type;
		box.setAttribute("dhxbox", 1);
			
		var inner = '';

		if (config.width)
			box.style.width = config.width;
		if (config.height)
			box.style.height = config.height;
		if (config.title)
			inner+='<div class="dhtmlx_popup_title">'+config.title+'</div>';
		inner+='<div class="dhtmlx_popup_text"><span>'+(config.content?'':config.text)+'</span></div><div  class="dhtmlx_popup_controls">';
		if (ok)
			inner += button(config.ok || "OK", true);
		if (cancel)
			inner += button(config.cancel || "Cancel", false);
		if (config.buttons){
			for (var i=0; i<config.buttons.length; i++)
				inner += button(config.buttons[i],i);
		}
		inner += '</div>';
		box.innerHTML = inner;

		if (config.content){
			var node = config.content;
			if (typeof node == "string") 
				node = document.getElementById(node);
			if (node.style.display == 'none')
				node.style.display = "";
			box.childNodes[config.title?1:0].appendChild(node);
		}

		box.onclick = function(e){
			e = e ||event;
			var source = e.target || e.srcElement;
			if (!source.className) source = source.parentNode;
			if (source.className == "dhtmlx_popup_button"){
				var result = source.getAttribute("result");
				result = (result == "true")||(result == "false"?false:result);
				callback(config, result);
			}
		};
		config.box = box;
		if (ok||cancel)
			_dhx_msg_cfg = config;

		return box;
	}
	function isSxhy(text){
		if(text){
			if(text.indexOf('双向核验')>=0){
				return true;
			}
		}
		return false;
	}
	function _createBox(config, ok, cancel){
		//var box = config.tagName ? config : _boxStructure(config, ok, cancel);
		var obj=$("#defaultwarningAlert_id")[0];
		var box=null;
		if(obj){
		   box=obj;
		   $("#content_defaultwarningAlert_title").html(config.title|| "");
		   $("#content_defaultwarningAlert_hearder").html(config.text|| "");
		   $("#content_defaultwarningAlert_body").html(config.body||"");
		   $("#qd_closeDefaultWarningWindowDialog_id").html(config.ok||"确定");
		}else 
		   box =$("<div id=\"defaultwarningAlert_id\">"
				+ "<div class=\"mark\"></div>"
				+ "<div class=\"up-box w600\" id=\"content_defaultwarningAlert_id\" style=\"border:#909090 0 solid;color:#333;box-shadow:2px 2px 10px #909090;\">"
				+ "<div class=\"up-box-hd\" ><span id=\"content_defaultwarningAlert_title\">"
				+ (config.title|| "")
				+ "</span><a href=\"javascript:\"id=\"gb_closeDefaultWarningWindowDialog_id\">关闭</a></div>"
				+ "<div class=\"up-box-bd\">"
				+ "<div class=\"up-con clearfix\">"
				+ "<span class=\"icon\" id=\"alert_icon_id\"></span>"
				+ " <div class=\"r-txt\">"
				+ "<div class=\"tit\" id=\"content_defaultwarningAlert_hearder\" >"
				+ (config.text|| "")
				+ "</div>"
				+ "<P  id=\"content_defaultwarningAlert_body\">"
				+ (config.body||"")
				+ "</P>"
				+ "</div>"
				+ "</div>"
				+ " <div class=\"lay-btn\"><a href=\"javascript:\" id=\"qd_closeDefaultWarningWindowDialog_id\" class=\"btn92s\">"
				+ (config.ok||"确定") + "</a></div>" + "</div>" + "</div>" + "</div>")[0];
			box.setAttribute("dhxbox", 1);
			$(box).css("z-index","20000");
			
			if(navigator.appVersion.indexOf("MSIE 6")>-1){
			}else{
			$(box).css("position","fixed");
			}
			config.box = box;
		if (!config.hidden)
			modality(true);
		document.body.appendChild(box);
		$('#alert_icon_id').removeClass().addClass('icon');
		if(isSxhy(config.text)){
			$('#alert_icon_id').addClass('ico-sxhy02');
		}else{
			$('#alert_icon_id').addClass('i-warn');
		}
		//var x = $(window).width() / 2 - 300;
		//var y =0;
		//if(getScrollTop()>0)
		//	y=($(window).height() / 2)-getScrollTop()/2;
		///else
		//	y=$(window).height() / 2 - 150;
		//var y = ($(window).height() / 2 - 200)-getScrollTop();
	    $(box).css({
	    	left: ($(window).width() - $(box).outerWidth())/2-300, 
	    	top: ($(window).height() - $(box).outerHeight())/2-110
	    }); 
		if(navigator.appVersion.indexOf("MSIE 6")>-1){
			$("#content_defaultwarningAlert_id").css({
	    	"margin-left":"-300px",
			"left":"50%",
			"margin-top":"-110px",
	    	"top": ($(window).height() - $(box).outerHeight())/2
	    }); }
        
		//necessary for IE only
		box.onkeydown = modal_key;

		box.focus();
		if (config.hidden)
			dhtmlx.modalbox.hide(box);

		$("#qd_closeDefaultWarningWindowDialog_id").on("click",function(e){
			callback(config);
			//阻止默认浏览器动作
		    if ( e && e.preventDefault ) 
		        e.preventDefault(); 
		    //IE中阻止函数器默认动作的方式 
		    else
		        window.event.returnValue = false; 
		});
		$("#gb_closeDefaultWarningWindowDialog_id").on("click",function(e){
			callback(config);
			//阻止默认浏览器动作
		    if ( e && e.preventDefault ) 
		        e.preventDefault(); 
		    //IE中阻止函数器默认动作的方式 
		    else
		        window.event.returnValue = false; 
		});
		if (ok||cancel)
			_dhx_msg_cfg = config;
		return box;
	}
	
	function getScrollTop() {
		if ('pageYOffset' in window) {
			return window.pageYOffset;
		} else if (document.compatMode == 'BackCompat') {
			return document.body.scrollTop;
		} else {
			return document.documentElement.scrollTop;
		}
	}

	function alertPopup(config){
		return _createBox(config, true, false);
	}
	function confirmPopup(config){
		return _createBox(config, true, true);
	}
	function boxPopup(config){
		return _boxPopup(config);
	}
	function _boxPopup(config){
		var box =$(config.targSrc)[0];
		box.setAttribute("dhxbox", 1);
		$(box).css("z-index","20000");
		config.box = box;
		if (!config.hidden)
			modality(true);
		document.body.appendChild(box);
//		var x = config.left||Math.abs(Math.floor(((window.innerWidth||document.documentElement.offsetWidth) - box.offsetWidth)/2));
//		var y = config.top||Math.abs(Math.floor(((window.innerHeight||document.documentElement.offsetHeight) - box.offsetHeight)/2));
//		if (config.position == "top")
//			box.style.top = "-3px";
//		else
//			box.style.top = y+'px';
//		box.style.left = x+'px';
		if(navigator.appVersion.indexOf("MSIE 6")>-1){
			$(box).css("position","absolute");
			$(box).css({
		    	"margin-left":"-300px",
				"left":"50%",
				"margin-top":"-110px",
		    	"top": ($(window).height() - $(box).outerHeight())/2
		    }); 
		}else{
		   $(box).css("position","fixed");
		   $(box).css({
		    	left: ($(window).width() - $(box).outerWidth())/2, 
		    	top: ($(window).height() - $(box).outerHeight())/2
		   });
		}
		//necessary for IE only
		box.onkeydown = modal_key;

		box.focus();
		if (config.hidden)
			dhtmlx.modalbox.hide(box);
		return box;
	};
	function box_params(text, type, callback){
		if (typeof text != "object"){
			if (typeof type == "function"){
				callback = type;
				type = "";
			}
			text = {text:text, type:type, callback:callback };
		}
		return text;
	}
	function params(text, type, expire, id){
		if (typeof text != "object")
			text = {text:text, type:type, expire:expire, id:id};
		text.id = text.id||t.uid();
		text.expire = text.expire||t.expire;
		return text;
	}
	dhtmlx.alert = function(){
		var text = box_params.apply(this, arguments);
		text.type = text.type || "confirm";
		return alertPopup(text);
	};
	dhtmlx.confirm = function(){
		var text = box_params.apply(this, arguments);
		text.type = text.type || "alert";
		return confirmPopup(text);
	};
	dhtmlx.modalbox = function(){
		var text = box_params.apply(this, arguments);
		text.type = text.type || "alert";
		return boxPopup(text);
	};
	dhtmlx.createWin = function(){
		var text = box_params.apply(this, arguments);
		text.type = text.type || "win";
		return boxWinPopup(text);
	};
	dhtmlx.modality=function(flag){
		modality(flag);
	};
	function boxWinPopup(config){
		//var box = config.tagName ? config : _boxStructure(config, ok, cancel);
		var box =$("#"+config.winId)[0];
		box.setAttribute("dhxbox", 1);
		$(box).css("z-index","20000");
		if(navigator.appVersion.indexOf("MSIE 6")>-1){
		}else{
		   $(box).css("position","fixed");
		}
		config.box = box;
		if (!config.hidden)
			modality(true);
		document.body.appendChild(box);
		var x = $(window).width() / 2 - 300;
		var y =0;
		if(getScrollTop()>0)
			y=($(window).height() / 2)-getScrollTop()/2;
		else
			y=$(window).height() / 2 - 150;
		//var y = ($(window).height() / 2 - 200)-getScrollTop();
		$(box).css("left",x+"px");
		$(box).css("top",y+"px");
		//necessary for IE only
		box.onkeydown = modal_key;

		//box.focus();
		$(box).show();
		if (config.hidden)
			dhtmlx.modalbox.hide(box);
		if(config.closeWinId){
			var _length=config.closeWinId.length;
			for(var k=0;k<_length;k++){
				$("#"+config.closeWinId[k]).unbind("click");
				$("#"+config.closeWinId[k]).on("click",function(e){
					var usercall = config.callback;
					modality(false);
					$(config.box).hide();
					_dhx_msg_cfg = config.box = null;
					if (usercall)
						usercall();
					//阻止默认浏览器动作
				    if ( e && e.preventDefault ) 
				        e.preventDefault(); 
				    //IE中阻止函数器默认动作的方式 
				    else
				        window.event.returnValue = false; 
				});	
			}
		}
		if(config.okId){
			$("#"+config.okId).unbind("click");
			$("#"+config.okId).on("click",function(e){
				if(config.checkConfirm){
					if(config.checkConfirm()){
						var usercall = config.okCallBack;
						modality(false);
						$(config.box).hide();
						_dhx_msg_cfg = config.box = null;
						if (usercall)
							usercall();
					}
				}else{
					var usercall = config.okCallBack;
					modality(false);
					$(config.box).hide();
					_dhx_msg_cfg = config.box = null;
					if (usercall)
						usercall();
				}
				//阻止默认浏览器动作
			    if ( e && e.preventDefault ) 
			        e.preventDefault(); 
			    //IE中阻止函数器默认动作的方式 
			    else
			        window.event.returnValue = false; 
			});	
		}
		return box;
	};
	dhtmlx.modalbox.hide = function(node){
		while (node && node.getAttribute && !node.getAttribute("dhxbox"))
			node = node.parentNode;
		if (node){
			node.parentNode.removeChild(node);
			modality(false);
		}
	};
	var t = dhtmlx.message = function(text, type, expire, id){
		text = params.apply(this, arguments);
		text.type = text.type||"info";

		var subtype = text.type.split("-")[0];
		switch (subtype){
			case "alert":
				return alertPopup(text);
			case "confirm":
				return confirmPopup(text);
			case "modalbox":
				return boxPopup(text);
			case "win":
				return boxWinPopup(text);
			default:
				return info(text);
			break;
		}
	};

	t.seed = (new Date()).valueOf();
	t.uid = function(){return t.seed++;};
	t.expire = 4000;
	t.keyboard = true;
	t.position = "top";
	t.pull = {};
	t.timers = {};

	t.hideAll = function(){
		for (var key in t.pull)
			t.hide(key);
	};
	t.hide = function(id){
		var obj = t.pull[id];
		if (obj && obj.parentNode){
			window.setTimeout(function(){
				obj.parentNode.removeChild(obj);
				obj = null;
			},2000);
			obj.className+=" hidden";
			
			if(t.timers[id])
				window.clearTimeout(t.timers[id]);
			delete t.pull[id];
		}
	};
})();